package com.adcc.monitoragent.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
	public static final String GRIB2 = "grib2";
	public static void main(String[] args) {
		String f = readFile("D:/ADCC/micapsdata/us_avn/wind/96/13120920.006");
		System.out.println(f.getBytes().length);
	}
	/**
	 * 创建路径
	 * @param path
	 */
	public static void creatPath(String path) {
		File f = new File(path);
		if (!f.exists()) {
            f.mkdirs();
			f.setWritable(true);// 设置允许写操作的访问权限
        }
	}
	
	/**
	 * 创建父路径
	 * @param path
	 */
	public static void creatParentPath(String path) {
		File file = new File(path);
	    if (!file.getParentFile().exists()) {
	        file.getParentFile().mkdirs();
	    }
	}
	
	/**
	 * 写数据到文件
	 * @param path 路径
	 * @param content 内容
	 * @param append
	 */
	public static void write(String path, String content, boolean append) {
		BufferedWriter output = null;
		try {
			File f = new File(path);
			if (f.exists()) {
				System.out.println("文件存在");
			} else {
				System.out.println("文件不存在，正在创建...");
				if (f.createNewFile()) {
					System.out.println("文件创建成功！");
				} else {
					System.out.println("文件创建失败！");
				}
			}
			output =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, append),"GBK")); 
			output.write(new String(content.getBytes()));
			output.close();
			output = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(output != null)
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取目录下的文件
	 * @param path
	 * @return
	 */
	public static List<File> readPathFiles(String path) {
		List<File> result = new ArrayList<File>();
		try {
			File file = new File(path);
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
			  if(!files[i].isDirectory()){
				  result.add(files[i]);
			  }
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public static String readFile(String path) {
		File f = new File(path);
		if (f.exists() && !f.isDirectory()) {
			return readFile(f);
		}
		return "";
	}
	
	/**
	 * 读取某个文件
	 * @param f
	 * @return
	 */
	public static String readFile(File f) {
		String s = new String();
		StringBuffer s1 = new StringBuffer();
		BufferedReader input = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(f));
			input = new BufferedReader(isr);
			while ((s = input.readLine()) != null) {
				s1.append(s).append("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return s1.toString();
	}
	
	/**
	 * 读取日志文件
	 * @param path
	 * @return
	 */
	public static List<String>  readLogFile(String path) {
		File f = new File(path);
		if (f.exists() && !f.isDirectory()) {
			return readLogFile(f);
		}
		return new ArrayList<String>();
	}
	/**
	 * 读取日志文件
	 * @param f
	 * @return
	 */
	public static List<String> readLogFile(File f) {
		List<String> list = new ArrayList<String>();
		String s = new String();
		BufferedReader input = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(f), "utf-8");
			input = new BufferedReader(isr);
			while ((s = input.readLine()) != null) {
				list.add(s.split("] - ")[1]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	
	/**
	 * 读取某个文件
	 * @param f
	 * @return
	 */
	public static InputStream readFileStream(String path) {
		InputStream input = null;
		File f = new File(path);
		if (f.exists() && !f.isDirectory()) {
			try {
				input = new FileInputStream(f);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}		
		return input;
	}
}
